/**
 * 
 */
package com.ly.hotel.view.common;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.util.Log;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.VideoView;

import com.ly.hotel.common.MyApplication;
import com.ly.hotel.model.Ad;
import com.ly.hotel.model.AdDetail;
import com.ly.hotel.util.ImageUtil;


/**
 * @author jouson
 * 2014-7-11
 */
public class AdBuilder {
	
	private Context context;
	private Ad ad;
	private Handler handler;
	ImageView btn;
	ImageButton button;
	VideoView vido;
	private int index=0;
	public AdBuilder(Context context, Ad ad,ImageView btn ) {
		this.context = context;
		this.ad = ad;
		this.btn = btn;
		index=1;
		this.handler= new Handler();
	}
	public AdBuilder(Context context, Ad ad,ImageButton button ) {
		this.context = context;
		this.ad = ad;
		this.button = button;
		index=3;
		this.handler= new Handler();
	}
	public AdBuilder(Context context, Ad ad,VideoView btn) {
		this.context = context;
		this.ad = ad;
		this.vido = btn;
		index=2;
		this.handler= new Handler();
	}
	
	
	public void build() {
		buildImageAnimation(ad.getDetails());
	}
	
	
	/**
	 * 构建图片轮播
	 * @param details
	 * @return
	 */
	public void buildImageAnimation(final List<AdDetail> details) {
//		Log.i("AdBuilder","构建图片轮播动画..."  + details.size());
        
        if (details.isEmpty()) {
        	return;
        }
        new Thread(new Runnable() {
			
			@Override
			public void run() {
				final List<Bitmap> imgs = new ArrayList<Bitmap>();
				final Map<Bitmap, Integer> imagesIntervalMap = new HashMap<Bitmap, Integer>();
				//加载图片
		        for (AdDetail detail : details) {
		        	Log.i("AdBuilder","加载图片go..." + detail.getPath() + ",间隔时间:" + detail.getSwitchTime() + "秒");
					Bitmap loadImage = ImageUtil.loadImage(context, MyApplication.getHostResUrl() + "upload/" + detail.getPath());
					if (loadImage != null) {
						imgs.add(loadImage);
						imagesIntervalMap.put(loadImage, detail.getSwitchTime()*1000);
					}
				}
		        Log.i("AdBuilder","图片轮播图片加成完成" + imgs.size()); 
		        if (imgs.isEmpty()) {
		        	return;
		        }
		        handler.post(new Runnable() {
					
					public void run() {
						AnimationDrawable ani = new AnimationDrawable();
						for (Bitmap bm : imgs) {
							ani.addFrame(new BitmapDrawable(context.getResources(), bm), imagesIntervalMap.get(bm));
						}
						ani.setOneShot(false);
						if(index==1)
						{
						btn.setImageDrawable(ani);
						}
						else if(index==2)
						{
							vido.setBackgroundDrawable(ani);
						}else if (index==3) {
							button.setBackgroundDrawable(ani);
						}
						ani.start();
						Log.i("AdBuilder","替换图片。。。。。。。。。。。");
					}
				});
		        
			}
		}).start();
        
	}
	
}


